package flex.cc.uac.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import flex.cc.base.dto.LoginAuthDto;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.uac.entity.UacCompanyEntity;
import flex.cc.uac.service.UacCompanyService;
import flex.cc.uac.vo.CompanyTreeVo;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 业务机构管理
 *
 * @author zhangzk
 * @email zhangzhikai@sinosoft.com.cn
 * @date 2020-07-11 22:29:37
 */
@RestController
@RequestMapping("/uac/company")
public class UacCompanyController extends BaseController {

    @Resource
    private UacCompanyService uacCompanyService;

    /**
     * 新增组织
     */
    @LogAnnotation
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增组织")
    public Wrapper<Integer> addUacCompany(@ApiParam(name = "company", value = "新增组织Dto") @RequestBody UacCompanyEntity company) {
        logger.info(" 新增组织 company={}", company);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        uacCompanyService.saveUacCompany(company, loginAuthDto);
        return WrapMapper.ok();
    }
    
    /**
     *  根据组织ID查询组织信息
     * */
    @ApiOperation(value = "根据组织ID删除组织")
    @PostMapping(value = "/delCompanyByCode/{comCode}")
    public Wrapper<Integer> delCompanyById(@ApiParam(name = "comCode", value = "组织ID") @PathVariable String comCode){
    	LoginAuthDto loginAuthDto = getLoginAuthDto();
    	uacCompanyService.delUacCompanyByCode(comCode, loginAuthDto);
        return WrapMapper.ok();
    }
    
    /**
     * 展示机构详情
     * @return
     */
    @GetMapping(value = "/showDetail/{comCode}")
    @ApiOperation(httpMethod = "get", value = "展示机构详情")
    public Wrapper<UacCompanyEntity> getUacCompany(@PathVariable String comCode) {
        UacCompanyEntity uacCompany = uacCompanyService.getOne(new QueryWrapper<UacCompanyEntity>().eq("com_code", comCode));
        return WrapMapper.ok(uacCompany);
    }
    
    /**
     * 获得机构树
     * @return
     */
    @GetMapping(value = "/getTree")
    @ApiOperation(httpMethod = "get", value = "获得机构树")
    public Wrapper<List<CompanyTreeVo>> getCompanyTree(@RequestParam(required = false) String comCode, String userFlag, String maxLevel) {
    	LoginAuthDto loginAuthDto = getLoginAuthDto();
    	// comCode可以为多个，用“,”隔开
    	String[] comCodes = comCode.split(",");
    	List<CompanyTreeVo> companyTreeVo = new ArrayList<CompanyTreeVo>();
    	for(String code : comCodes) {
    		companyTreeVo.addAll(uacCompanyService.getCompanyTreeVo(code, maxLevel, userFlag, loginAuthDto));
    	}
        return WrapMapper.ok(companyTreeVo);
    }



    /**
     * 获得单个机构
     * @return
     */
    @GetMapping(value = "/getComInfo")
    @ApiOperation(httpMethod = "get", value = "获得单个机构")
    public Wrapper<List<CompanyTreeVo>> getCompany(@RequestParam(required = true) String comCode) {
    	List<CompanyTreeVo> companyVo = uacCompanyService.getCompanyVo(comCode);
        return WrapMapper.ok(companyVo);
    }
    
    /**
     * 根据机构获取所有父级
     */
    @GetMapping(value = "/findComPath")
    @ApiOperation(httpMethod = "get", value = "根据机构获取所有父级")
    public Wrapper<Map<String, Object>> findComPath(@RequestParam(required = true) String comCode, String maxLevel, String userFlag) {
    	LoginAuthDto loginAuthDto = getLoginAuthDto();
    	Map<String, Object> map = uacCompanyService.findComPathList(comCode, maxLevel, userFlag, loginAuthDto);
        return WrapMapper.ok(map);
    }

}
